<template>
    <div>
        <!--列表-->
        <template>
            <el-table v-loading="listLoading" :data="tableData" style="width: 100%"
                      @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column v-for="item in tableTile" :prop="item.key" :label="item.name"></el-table-column>
            </el-table>
        </template>
        <!--分页-->
        <el-col :span="24" class="toolbar" style="padding-bottom:10px;">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageParm.currentPage"
                    :page-sizes="[5, 10, 20, 50]"
                    :page-size="pageParm.page_size"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageParm.count" style="float:right">
            </el-pagination>
        </el-col>
    </div>
</template>

<script>
  import dao from '../../common/dao'
  import util from '../../common/util'

  export default {
    props: {
      type: {
        type: String,
        default: 'hdfs'
      }
    },
    data() {
      return {
        ptype: this.type,
        tableTile: [],
        tableData: [],
        pageParm: {
          "count": 0, // 总条数
          "page_size": 20, // 每页条数
          "page_count": 0, // 总页数
          "currentPage": 1 // 当前页数
        },
        tenantIds: "",
        tenantArray: [],
        multipleSelection: [],
        butStatus: {
          modify: true,
          deploy: true
        },
        queueList: []
      }
    },
    methods: {
      pushDateTop (val) {
        this.$emit('change', val);
      },
      handleSizeChange (val) {
        this.pageParm.page_size = val;
        this.getTableTitle();
      },
      handleCurrentChange (val) {
        this.pageParm.currentPage = val;
        this.getTableTitle();
      },
      getTenantIds(){
        let tenantIdArray = [];
        let type = this.type.toUpperCase();
        if (type !== "") {
          this.tenantArray.map(tenant => {
            if (("," + tenant.tenant_plugins + ",").toUpperCase().indexOf(type) > -1) {
              tenantIdArray.push(tenant.tenant_id);
            }
          });
        }
        this.tenantIds = tenantIdArray.toString();
      },
      handleSelectionChange (val) {
        this.multipleSelection = val
        this.pushDateTop(val);
      },
      getSelectedForm (type) {
      },
      async getTableTitle () {
        this.tenantArray = util.fetch('page42_tenantArray');
        this.getTenantIds();
        //清空勾选的记录
        this.multipleSelection = [];
        this.pushDateTop(this.multipleSelection);
        let tenantIds = this.tenantIds;
        if (tenantIds === "") return false;

        let body = null;
        switch (this.ptype) {
          case "hdfs":
            this.tableTile = [{
              key: 'tenant_name',
              name: '租户名字'
            }, {
              key: 'hdfs_dir',
              name: '目录'
            }, {
              key: 'hdfs_files_used_sizes',
              name: '已用空间(TB/TB)'
            }, /*{
             key:'lastSpace',
             name:'剩余空间(TB)'
             },*/{
              key: 'hdfs_files_used_num',
              name: '文件目录数'
            }];
            body = {
              start_index: this.pageParm.currentPage,
              page_count: this.pageParm.page_size,
              "tenant_id": tenantIds
            };
            dao.getHdfsInfoList(body).then((response) => {
              if (!response.body || response.body.retCode != 200) {
                return false;
              }
              let pageRes = response.body;
              let results = pageRes.results;
              let tableDataALl = [];
              results.map(result => {
                if (result.tenant_resource) {
                  result.tenant_resource.map(resource => {
                    resource.tenant_name = result.tenant_info.tenant_name;
                    resource.hdfs_files_used_sizes = resource.hdfs_files_sizes_used + '/' + resource.hdfs_files_sizes;
                    resource.hdfs_files_used_num = resource.hdfs_files_num_used + '/' + resource.hdfs_files_num;
                    tableDataALl.push(resource);
                  });
                }
              });
              this.tableData = tableDataALl;
              this.pageParm.count = parseInt(pageRes.count);
            }, (response) => {
            });
            break;
          case "yarn":
            await this.getQueueOptions();
            this.tableTile = [{
              key: 'tenant_name',
              name: '租户名字'
            }, {
              key: 'yarn_queue_name',//name
              name: '队列'
            }, {
              key: 'resource_percent',//capacity
              name: '占用资源(%)'
            }, {
              key: 'resource_percent_max',//maxCapacity
              name: '最大资源(%)'
            }, {
              key: 'maxApplications',
              name: '最大应用数量'
            }, {
              key: 'userLimit',
              name: '每个用户最低资源保障(%)'
            }];
            body = {
              start_index: this.pageParm.currentPage,
              page_count: this.pageParm.page_size,
              "tenant_id": tenantIds
            };
            dao.getYarnInfoList(body).then((response) => {
              if (response.FX.err) return false;

              let pageRes = response.body;
              let results = pageRes.results;
              let tableDataALl = [];
              results.map(result => {
                if (result.tenant_resource) {
                  result.tenant_resource.map(resource => {
                    resource.tenant_name = result.tenant_info.tenant_name;
                    tableDataALl.push(resource);
                  })
                  tableDataALl = result.tenant_resource;
                }
              });
              this.tableData = tableDataALl;
              this.pageParm.count = parseInt(pageRes.count);
            }, (response) => {
            });
            break;
          case "hive":
            this.tableTile = [{
              key: 'tenant_name',
              name: '租户名字'
            }, {
              key: 'hive_database_name',
              name: '数据库'
            }, {
              key: 'hive_dir',
              name: '数据库目录'
            }, {
              key: 'hive_files_used_sizes',
              name: '已用空间(TB/TB)'
            }, {
              key: 'hive_files_used_num',
              name: '表数量'
            }]
            body = {
              start_index: this.pageParm.currentPage,
              page_count: this.pageParm.page_size,
              "tenant_id": tenantIds
            };
            dao.getHiveInfoList(body).then((response) => {
              if (!response.body || response.body.retCode != 200) {
                return false;
              }
              let pageRes = response.body;
              let results = pageRes.results;
              let tableDataALl = [];
              results.map(result => {
                if (result.tenant_resource) {
                  result.tenant_resource.map(resource => {
                    resource.tenant_name = result.tenant_info.tenant_name;
                    resource.hive_files_used_sizes = resource.hive_files_sizes_used + '/' + resource.hive_files_sizes;
                    resource.hive_files_used_num = resource.table_num + '/' + resource.hive_files_num;
                    tableDataALl.push(resource);
                  });
                }
              });
              this.tableData = tableDataALl;
              this.pageParm.count = parseInt(pageRes.count);
            }, (response) => {
              // error callback
              console.log(response)
            });
            break;
          case "hbase":
            this.tableTile = [{
              key: 'tenant_name',
              name: '租户名字'
            }, {
              key: 'hbase_tablespace_name',
              name: '表空间'
            }, {
              key: 'hbase_tables_used_max_num',
              name: '已建表数量'
            }, {
              key: 'hbase_regions_used_max_num',
              name: 'region数量'
            }, {
              key: 'hbase_qps',
              name: 'QPS(次/s)'
            }, {
              key: 'hbase_ops',
              name: 'OPS(M/s)'
            }]
            body = {
              start_index: this.pageParm.currentPage,
              page_count: this.pageParm.page_size,
              "tenant_id": tenantIds
            };
            dao.getHbaseInfoList(body).then((response) => {
              if (!response.body || response.body.retCode != 200) {
                return false;
              }
              let pageRes = response.body;
              let results = pageRes.results;
              let tableDataALl = [];
              results.map(result => {
                if (result.tenant_resource) {
                  result.tenant_resource.map(resource => {
                    resource.tenant_name = result.tenant_info.tenant_name;
                    resource.hbase_tables_used_max_num = resource.hbase_tables_max_num_used + '/' + resource.hbase_tables_max_num;
                    resource.hbase_regions_used_max_num = resource.hbase_regions_max_num_used + '/' + resource.hbase_regions_max_num;
                    tableDataALl.push(resource);
                  });
                }
              });
              this.tableData = tableDataALl;
              this.pageParm.count = parseInt(pageRes.count);
            }, (response) => {
              // error callback
              console.log(response)
            });
            break;
          case "kafka":
            this.tableTile = [{
              key: 'tenant_name',
              name: '租户名字'
            }, {
              key: 'kafka_topic_name',
              name: '主题'
            }, {
              key: 'kafka_topic_partitions_num',
              name: '分区数'
            }, {
              key: 'kafka_topic_backup_num',
              name: '备份数'
            }]
            body = {
              start_index: this.pageParm.currentPage,
              page_count: this.pageParm.page_size,
              "tenant_id": tenantIds
            };
            dao.getKafkaInfoList(body).then((response) => {
              if (!response.body || response.body.retCode != 200) {
                return false;
              }
              let pageRes = response.body;
              let results = pageRes.results;
              let tableDataALl = [];
              results.map(result => {
                if (result.tenant_resource) {
                  result.tenant_resource.map(resource => {
                    resource.tenant_name = result.tenant_info.tenant_name;
                    tableDataALl.push(resource);
                  });
                }
              });
              this.tableData = tableDataALl;
              this.pageParm.count = parseInt(pageRes.count);
            }, (response) => {
              // error callback
            });
            break;
        }
      },
      async getQueueOptions(){
        let ret = await dao.getQueueList({}).then(res => {
          if (res.FX.err) {
            return false
          }
          this.queueList = res.body.queueList;
          return true
        },()=>{
          return true
        });
        return ret;
      }
    },
    mounted () {
      this.getTableTitle();
    }
  }
</script>

<style scoped>
    .toolbar .el-form-item {
        margin-bottom: 10px;
    }

    .toolbar {
        background: #fff;
        padding: 10px 10px 0px 10px;
    }
</style>